package com.alpaak.hellospringboot.controller;


import com.alpaak.hellospringboot.entity.User;
import com.alpaak.hellospringboot.modle.qo.CommonQo;
import com.alpaak.hellospringboot.service.IUserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author alpaak
 * @since 2021-12-22
 */
@Api(tags = "用户接口")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @ApiOperation(value = "根据用户id获得用户", notes = "")
    @GetMapping("/getUser")
    public User getUser(Integer id) {
        return userService.getById(id);
    }

    @ApiOperation(value = "分页查询用户", notes = "")
    @GetMapping("/getUserPage")
    public IPage<User> getUserPage(CommonQo qo) {
//        第几页
        int current = qo.getCurrent();
//        当前面
        int size = qo.getSize();
        Page<User> page = new Page<>(current, size);
        return userService.page(page);
    }

    @ApiOperation(value = "保存用户", notes = "")
    @PostMapping("/addUser")
    public String addUser(@RequestBody User user) {
        boolean count = userService.saveOrUpdate(user);
        if (count) {
            return "保存成功";
        } else {
            return "保存失败";
        }
    }

    @ApiOperation(value = "删除用户", notes = "")
    @PostMapping("/delUser")
    public String delUser(Integer id) {
        boolean count = userService.removeById(id);
        if (count) {
            return "删除成功";
        } else {
            return "删除失败";
        }
    }
}
